package provider.feignParam;

import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import libra.model.JsonResult;
import libra.model.Provider_Pay;

/**
 * 测试使用OpenFeign的
 * @author libra
 */
@RestController
@RequestMapping("feignParam")
public class FeignParamController {
	
	@Value( "#{'${server.servlet.context-path}'.concat('[port(${server.port})]')}" )
	String identity;
	
	/**
	 * 测试Consumer端的OpenFeign的请求超时控制
	 */
	@RequestMapping("feignTimeOut")
	public Object feignTimeOut( HttpServletRequest req ) throws Exception{
		String str = LocalTime.now().toString() + " -> ";
		try {
			Thread.sleep(5000);
		} catch (Exception e) {
			e.printStackTrace();
		}
		str += LocalTime.now().toString();
		return new JsonResult<String>(0, identity, str);
	}
	
	// 测试基础类型传参
	/**
	 * 测试Get请求传递参数(基础参数类型)
	 */
	@GetMapping("getParam")
	public Object getParam( Integer getParam ) {
		return new JsonResult<Object>(0, identity, new HashMap<String, Object>(){{ 
			put("uri", "getParam");
			put("paraType", getParam.getClass());
			put("param", getParam);
			}}   
		);
	}
	/**
	 * 测试Post请求传递参数(基础参数类型)
	 */
	@PostMapping("postParam")
	public Object postParam( Double postParam ) {
		return new JsonResult<Object>(0, identity, new HashMap<String, Object>(){{ 
			put("uri", "postParam");
			put("paraType", postParam.getClass());
			put("param", postParam);
			}}   
		);
	}

	
	// 测试restful传参
	/**
	 * 测试Get请求RestFul传递path参数
	 */
	@GetMapping("getPath/{getPathParam}")
	public Object getPath( @PathVariable("getPathParam")String param ) {
		return new JsonResult<Object>(0, identity, new HashMap<String, Object>(){{ 
			put("uri", "getPath/{getPathParam}");
			put("paraType", param.getClass());
			put("param", param);
			}}   
		);
	}
	/**
	 * 测试Post请求RestFul传递path参数
	 */
	@PostMapping("postPath/{postPathParam}")
	public Object postPath( @PathVariable("postPathParam")Boolean param ) {
		return new JsonResult<Object>(0, identity, new HashMap<String, Object>(){{ 
			put("uri", "postPath/{postPathParam}");
			put("paraType", param.getClass());
			put("param", param);
			}}   
		);
	}
	
	
	// 测试复杂传参
	/**
	 * 测试Post请求传递json
	 */
	@PostMapping("postJson")
	public Object postJson( @RequestBody Map<String, Object> json ) {
		return new JsonResult<Object>(0, identity, new HashMap<String, Object>(){{ 
			put("uri", "postJson");
			put("param", json);
			}}   
		);
	}
	/**
	 * 测试Post请求传递参数(基础参数类型)
	 */
	@PostMapping("postObj")
	public Object postObj( Provider_Pay obj ) {
		return new JsonResult<Object>(0, identity, new HashMap<String, Object>(){{ 
			put("uri", "postParam");
			put("param", obj);
			}}   
		);
	}
}
